<?php
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class NewsController extends Controller{
	public $assign = [];
	public function newsIndex(){
		$model_news = new \App\Model\News();
		$info = $model_news->getNews();
		$this ->assign['info'] = $info;
		return view('admin.new.newsindex',$this->assign);
	}
	//添加新闻
	public function addNews(){
		return view('admin.new.addnews',$this->assign);
	}
	//执行添加
	public function doAddNews(Request $request){
		$res = $request->all();
		$title = $res['title'];
		$describe = $res['describe'];
		$content = $res['content'];
		$createtime = date('Y-m-d H:i:s',time());
		
		$model_news = new \App\Model\News();
		$info = $model_news->createNews($title,$describe,$content,$createtime);
		if (!empty($info)) {
			echo "<script>alert('添加成功');window.location.href='/admin/news';</script>";
		} else {
			echo "<script>alert('添加失败');window.location.href='/admin/addnews'</script>";
		}
	}
	//编辑
	public function edit($id){
		$model_news = new \App\Model\News();
		$info = $model_news->getNewById($id);
		$this->assign['info'] = $info;
		return view('admin.new.editnews',$this->assign);
	}
	//执行编辑
	public function doeditnews(Request $request,$id){
		$res = $request->all();
		$ttile = $res['title'];
		$describe = $res['describe'];
		$content = $res['content'];
		$createtime = date('Y-m-d H:i:s',time());
		$model_news = new \App\Model\News();
		$info = $model_news->updateNews($id,$ttile,$describe,$content,$createtime);
		if($info == 1){
			echo '<script>alert("添加成功");window.location.href="/admin/news"</script>';
		}
		echo  '<script>alert("添加失败");window.location.href="/admin/news/edit/{id}"</script>';
		
	}
	//删除
	public function delNews(Request $request){
		$id = $request ->post('id');
		$model_news = new \App\Model\News();
		$info = $model_news->delOne($id);
	}





}




















?>